//get all intersection points between the WorkLine and lines: [TL, TR, BL, BR] - algorithm imporved
function findIntersection(TL, TR, BL, BR, WorkLines) {
	var shortLines = new Array();
	var lineIcpts = new Array();
	
	shortLines.push(splitPolyline(TL));
	shortLines.push(splitPolyline(BL));
	shortLines.push(splitPolyline(TR));	
	shortLines.push(splitPolyline(BR));
	
	for(var lineNum = 0; lineNum < shortLines.length; lineNum++) {
		var linePortions = shortLines[lineNum];
		var tmpIcpt = new Array();
		
		for (var i = 0; i < linePortions.length; i++) {
			var maxx = Math.max(linePortions[i][0][0], linePortions[i][1][0]);
			var minx = Math.min(linePortions[i][0][0], linePortions[i][1][0]);
			var maxy = Math.max(linePortions[i][0][1], linePortions[i][1][1]);
			var miny = Math.min(linePortions[i][0][1], linePortions[i][1][1]);
			for (var j = 0; j < WorkLines.length ; j++) {
				var icptObj = getLineIntersectionData(linePortions[i], WorkLines[j]);
				if(icptObj !== undefined) {
					var line1 = icptObj.line1_data;
					var icpt = [icptObj.icptX.toFixed(3), icptObj.icptY.toFixed(3)];
					//if the intersection point is on the line
					if(icpt[0] >= minx && icpt[0] <= maxx && icpt[1] >= miny && icpt[1] <= maxy) {
						//if the workline is polyline
						if (WorkLines.length > 1) {
							//only collect the intersections of same side lines, left with left, right with right.							
							if((lineNum < 2 && j < Math.round(WorkLines.length / 2) && icpt[0] < 0) || (lineNum >= 2 && j >= Math.round(WorkLines.length / 2) && icpt[0] > 0)) {
								tmpIcpt.push(icpt);
							}
						}
						//workline is one straight line
						else {
							if((lineNum < 2 && icpt[0] < 0) || (lineNum >= 2 && icpt[0] > 0)) {
								tmpIcpt.push(icpt);
							}
						}
					}
				}
			}
		}
		
		lineIcpts.push(tmpIcpt);
	}
	
	return {tlicpt:lineIcpts[0], blicpt:lineIcpts[1], tricpt:lineIcpts[2], bricpt:lineIcpts[3]};
}

//split the polyline into multiple straight lines made of only two points
function splitPolyline(polyline) {
	var splitedLines = new Array();
	for (var i = 0; i < polyline.length - 1; i++) {
		var startPoint = polyline[i];
		var endPoint = polyline[i + 1];
		splitedLines.push([startPoint, endPoint]);
	}
	return splitedLines;
}

//convert the intersection point object to a xy data array can be used in flot
function convertToFlotLineArray(icptObj, workPointArray) {	
	var icptArray = new Array();
	
	for (var x in icptObj) {
		for(var i = 0; i < icptObj[x].length; i++) {
			icptArray.push(icptObj[x][i]);
		}		
	}
	for(var i = 0; i < workPointArray.length; i++) {
		if(workPointArray[i][0] == 0)
		icptArray.push(workPointArray[i]);
	}
	icptArray.sort(function(a,b){return a[0] - b[0];});
	
	return icptArray;
}

//convert the intersection point object to a xy data array can be used in flot
function convertToFlotLineArray_old(icptObj, workPointArray) {	
	var icptArray = new Array();
	//Number of work points less than 3, which means only one line is drawn
	if (workPointArray.length < 3) {
		for (var x in icptObj) {
			for(var i = 0; i < icptObj[x].length; i++) {
				icptArray.push(icptObj[x][i]);
			}		
		}
	}
	//Number of work points is 3 or more, the work line is a polyline
	//else if (workPointArray.length >= 3 && icptObj.tlicpt.length == 1 && icptObj.tricpt.length == 1) {
	else if (icptObj.tlicpt.length == 1 && icptObj.tricpt.length == 1) {
		icptArray.push(icptObj.tlicpt[0]);
		for(var i = 0; i < workPointArray.length; i++) {
		if(workPointArray[i][0] == 0)
		icptArray.push(workPointArray[i]);
	}
	icptArray.push(icptObj.tricpt[0]);
	}
	else {
		for (var x in icptObj) {
			for(var i = 0; i < icptObj[x].length; i++) {
				icptArray.push(icptObj[x][i]);
			}		
		}
		for(var i = 0; i < workPointArray.length; i++) {
			if(workPointArray[i][0] == 0)
			icptArray.push(workPointArray[i]);
		}
		icptArray.sort(function(a,b){return a[0] - b[0];});
	}
	
	return icptArray;
}	

//Based on the number of work points supplied, generate the worklines array.
//Each part of the array is a straight line only with start/end point
function generateWorklines(workPointArray, maxX, minX, midBaseY) {
	var worklines = new Array();
	
	switch (workPointArray.length) {
		case 0: 
		return worklines;
		break;
		case 1:
		worklines.push([[minX, workPointArray[0][1]], [maxX, workPointArray[0][1]]]);
		break;
		default:
		var y0 = workPointArray[0][1];
		var y1 = workPointArray[workPointArray.length - 1][1];
		worklines.push([[minX, y0], [maxX, y0]]);
		worklines.push([[minX, y1], [maxX, y1]]);
	}
	
	return worklines;
}